<?php
// +----------------------------------------------------------------------------
// | Tke团队版权所属 [三十年河东三十年河西,莫欺少年穷.!]
// +----------------------------------------------------------------------------
// | Copyright (c) 2014 http://www.itdaodan.com All rights reserved.
// +----------------------------------------------------------------------------
// | Author: Cruise<jiangyu@hrbjjhh.com>
// +----------------------------------------------------------------------------
/**
 +------------------------------------------------------------------------------
* QA模型
+------------------------------------------------------------------------------
* @version   1.0
* @since 2016-03-12
+------------------------------------------------------------------------------
*/
namespace Home\Model;

class AskModel {

    public $error;//记录最后一次错误信息
    
    /**
     * @todo 搜索数据
     * @param array    $map where 条件
	 *                 如果需要某个位置的商品需要传入 字段position 类型 int 例如 $map['position'] = 1;
	 *                 如果需要跟个位置的商品需要传入 字段position 类型 array 例如  $map['position'] = array(1,2,3);
	 * @param int      $page 页码
	 * @param int      $page_count 每页条数
	 * @param string   $field 需要查询的字段
	 * @param string   $order 排序
     * @return array|boolean
     * @author Cruise <jiangyu@hrbjjhh.com>
     */
    public function getArticleList($map, $limit = 0, $page = 1, $page_count = 10, $field = true, $order = 'sort desc, ask_article_id desc'){
    	$page = (int)$page == 0 ? 1 : (int)$page;
    	$limit = (int)$limit;
    	$page_count = (int)$page_count;
    	if(empty($map)){
    		$this->error = '条件错误！';
    	}
    	$map['status'] = 1;
    	$obj = D('Admin/ServiceApi');
    	$article_list = $obj->getArticleList($map,$limit,$page,$page_count);
    	if(empty($article_list)){
    		return null;
    	}
    	$article_category = $obj->getAskCategory();
    	foreach ($article_list as $key=>$val){
    		if($val['ask_category_id'] == $article_category[$val['ask_category_id']]['ask_category_id']){
    			$article_list[$key]['ask_category_name'] = $article_category[$val['ask_category_id']]['ask_category_name'];
    		}
    		if($val['ask_category_pid'] == $article_category[$val['ask_category_pid']]['ask_category_id']){
    			$article_list[$key]['ask_category_pname'] = $article_category[$val['ask_category_pid']]['ask_category_name'];
    		}
    	}
    	return $article_list;
    }
    
    /**
     * @todo 文章详情页数据
     * @param int     $id  分类id
     * @return array|boolean
     * @author Cruise <jiangyu@hrbjjhh.com>
     */
    public function getArticleInfo($id){
    	$ask_article_id = (int)$id;
    	if($ask_article_id == 0){
    		$this->error = '文章id有误！';
    		return false;
    	}
    	$article_obj = D('Admin/ServiceApi');
    	$article_info = $article_obj->getArticleInfo($ask_article_id);
    	if(empty($article_info)) {
    		$this->error = '暂无文章信息！';
    		return false;
    	}
    	//获取分类名称
    	$two_category = $article_obj->getAskCategory($article_info['ask_category_id']);
    	$first_category = $article_obj->getAskCategory($article_info['ask_category_pid']);
    	$article_info['ask_category_name'] = !empty($two_category) ? $two_category['ask_category_name'] : null;
    	$article_info['ask_category_pname'] = !empty($first_category) ? $first_category['ask_category_name'] : null;
    	
    	return $article_info;
    }
    
    /**
     * @todo 返回最后一次错误信息
     * @return string
     * @author Cruise <jiangyu@jjhh.com>
     */
    public function getError(){
    	return $this->error;
    }
    
}
